package org.dromara.exam.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.exam.domain.bo.ExamAnswerBo;
import org.dromara.exam.domain.bo.ExamProjectBo;
import org.dromara.exam.domain.vo.ExamProjectVo;
import org.dromara.exam.domain.vo.PublicPaperVo;
import org.dromara.exam.service.IExamProjectService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目
 *
 * @author xiyin321
 * @date 2024-05-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/exam/project/")
public class ExamProjectController extends BaseController {

    private final IExamProjectService examProjectService;

    /**
     * 分页查询项目
     */
    @SaCheckPermission(value = {"exam:project:list", "exam:project:query"}, mode = SaMode.OR)
    @GetMapping("page")
    public TableDataInfo<ExamProjectVo> page(ExamProjectBo bo, PageQuery pageQuery) {
        return examProjectService.queryPage(bo, pageQuery);
    }

    /**
     * 查询项目列表
     */
    @SaCheckPermission("exam:project:query")
    @GetMapping("list")
    public R<List<ExamProjectVo>> list(ExamProjectBo bo) {
        return R.ok(examProjectService.queryList(bo));
    }

    /**
     * 导出项目列表
     */
    @SaCheckPermission("exam:project:export")
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("export")
    public void export(ExamProjectBo bo, HttpServletResponse response) {
        List<ExamProjectVo> list = examProjectService.queryList(bo);
        ExcelUtil.exportExcel(list, "项目", ExamProjectVo.class, response);
    }

    /**
     * 获取项目详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("exam:project:query")
    @GetMapping("{id}")
    public R<ExamProjectVo> getInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(examProjectService.queryById(id));
    }

    /**
     * 新增项目
     */
    @SaCheckPermission("exam:project:add")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<ExamProjectVo> add(@Validated(AddGroup.class) @RequestBody ExamProjectBo bo) {
        return R.ok(examProjectService.insertByBo(bo));
    }

    /**
     * 修改项目
     */
    @SaCheckPermission("exam:project:edit")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ExamProjectBo bo) {
        return toAjax(examProjectService.updateByBo(bo));
    }

    /**
     * 项目重命名
     */
    @SaCheckPermission("exam:project:edit")
    @Log(title = "项目重命名", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("rename")
    public R<Void> rename(@RequestBody ExamProjectBo bo) {
        return toAjax(examProjectService.updateName(bo));
    }

    /**
     * 删除项目
     *
     * @param ids 主键串
     */
    @SaCheckPermission("exam:project:remove")
    @Log(title = "项目", businessType = BusinessType.DELETE)
    @DeleteMapping("{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(examProjectService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 发布项目
     *
     * @param id 项目id
     */
    @SaCheckPermission("exam:project:publish")
    @Log(title = "发布项目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("publish/{id}")
    public R<Void> publishPaper(@Validated(EditGroup.class) @PathVariable Long id) {
        examProjectService.publishProject(id);
        return toAjax(true);
    }

    /**
     * 停止项目
     *
     * @param id 项目id
     */
    @SaCheckPermission("exam:project:publish")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("cancelPublish/{id}")
    public R<Void> cancelPublish(@Validated(EditGroup.class) @PathVariable Long id) {
        examProjectService.cancelPublishProject(id);
        return toAjax(true);
    }

    /**
     * 预览项目
     * @param id
     * @return
     */
    @SaCheckPermission("exam:project:view")
    @GetMapping("preview/{id}")
    public R<PublicPaperVo> view(@NotNull(message = "主键不能为空")
                                 @PathVariable Long id) {
        return R.ok(examProjectService.queryViewById(id));
    }

    /**
     * 查询个人参与的项目列表
     * @param pageQuery
     * @return
     */
    @GetMapping("partake")
    public TableDataInfo<ExamProjectVo> listByUserId( ExamProjectBo bo, PageQuery pageQuery) {
        return examProjectService.queryProjectsPageByUserId(bo, pageQuery);
    }
}
